import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import UnoCSS from 'unocss/vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import { VitePWA } from 'vite-plugin-pwa'

// https://vitejs.dev/config/
/** @type {import('vite').UserConfig} */
export default defineConfig({
  envPrefix: 'SP',
  plugins: [
    vue({
      template: {
        compilerOptions: { isCustomElement: (tag) => tag.startsWith('wc-') }
      }
    }),
    vueJsx(),
    UnoCSS(),
    // basicSsl(),
    AutoImport({
      imports: [
        'vue',
        'vue-router',
        {
          'naive-ui': [
            'useDialog',
            'useMessage',
            'useNotification',
            'useLoadingBar'
          ]
        }
      ],
      // 自动导入 vue 的 API，eg: ref | reactive | computed
      dts: 'src/auto-import.d.ts'
    }),
    Components({
      resolvers: [NaiveUiResolver()]
    }),
    VitePWA(
      {
        registerType: 'autoUpdate',
        injectRegister: 'auto',
        workbox: {
          globPatterns: ['**/*.{js,css,html,ico,png,jpg,jpeg,webp,svg}']
        },
        injectManifest: {
          globPatterns: ['**/*.{js,css,html,ico,png,jpg,jpeg,webp,svg}']
        },
        manifest: {
          name: 'Spellai',
          short_name: 'Spellai',
          description: 'Instantly generate art photos and create unique avatars with AI magic!',
          display: 'standalone',
          theme_color: '#000000',
          background_color: '#000000',
          icons: [
            {
              'src': '/icons/pwa-icon-64x64.png',
              'sizes': '64x64',
              'type': 'image/png'
            },
            {
              'src': '/icons/pwa-icon-192x192.png',
              'sizes': '192x192',
              'type': 'image/png'
            },
            {
              'src': '/icons/pwa-icon-512x512.png',
              'sizes': '512x512',
              'type': 'image/png'
            },
            {
              'src': '/icons/maskable-icon-512x512.png',
              'sizes': '512x512',
              'type': 'image/png',
              'purpose': 'maskable'
            }
          ]
        },
        devOptions: {
          enabled: true,
          type: 'module'
        }
      }
    )
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    // host: true,
    // proxy: {
    //   '/spell': {
    //     target: 'https://sptv2.aimirror.fun',
    //     changeOrigin: true
    //   }
    // }
  }
})
